const gulp = require("gulp");
const sass = require("gulp-sass");
const babel = require("gulp-babel");
const concat = require("gulp-concat");
const uglify = require("gulp-uglify-es").default;
const rename = require("gulp-rename");
const cleanCSS = require("gulp-clean-css");
const sequence = require("gulp-sequence");
const i18n = require("gulp-html-i18n");
const browserSync = require("browser-sync").create();
const filter = require("gulp-filter");
const autoprefixer = require("gulp-autoprefixer");
const imagemin = require("gulp-imagemin");
const del = require("del");
const runSequence = require("run-sequence");

const { reload } = browserSync;

const DEFAULT_LANG = "zh";

const paths = {
  src: "src/",
  lang: "src/i18n/",
  dest: "dist/",
  templates: {
    src: "src/**/*.html",
    dest: "dist/"
  },
  styles: {
    src: "src/styles/**/*.scss",
    dest: "dist/styles/"
  },
  scripts: {
    src: "src/scripts/**/*.js",
    dest: "dist/scripts/"
  },
  images: {
    src: "src/images/**",
    dest: "dist/images/"
  },
  data: {
    src: "src/data/**/*.json",
    dest: "dist/data/"
  }
};

function clean() {
  return del(["dist"]);
}

function templates() {
  return gulp
    .src(paths.templates.src)
    .pipe(
      i18n({
        langDir: paths.lang,
        trace: true
        // inline: 'en'
      })
    )
    .pipe(
      rename(function(obj) {
        // 默认语言为中文
        obj.basename = obj.basename.replace(`-${DEFAULT_LANG}`, "");
      })
    )
    .pipe(gulp.dest(paths.templates.dest));
}

function styles() {
  return gulp
    .src(paths.styles.src)
    .pipe(sass().on("error", sass.logError))
    .pipe(
      autoprefixer(
        "last 2 version",
        "> 1%",
        "safari 5",
        "ie 8",
        "ie 9",
        "opera 12.1",
        "ios 6",
        "android 4"
      )
    )
    .pipe(cleanCSS())
    .pipe(
      rename({
        // basename: 'main',
        // suffix: '.min',
        suffix: ""
      })
    )
    .pipe(gulp.dest(paths.styles.dest))
    .pipe(filter("**/*.css")) // Filtering stream to only css files
    .pipe(browserSync.reload({ stream: true }));
}

function scripts() {
  return (
    gulp
      .src(paths.scripts.src)
      .pipe(babel())
      .pipe(uglify())
      // .pipe(concat('main.min.js'))
      .pipe(gulp.dest(paths.scripts.dest))
  );
}

function images() {
  return gulp
    .src(paths.images.src)
    .pipe(imagemin())
    .pipe(gulp.dest(paths.images.dest));
}

function originalImages() {
  return gulp.src(paths.images.src).pipe(gulp.dest(paths.images.dest));
}

function data() {
  return gulp.src(paths.data.src).pipe(gulp.dest(paths.data.dest));
}

function serve() {
  browserSync.init({
    // baseDir: paths.src
    server: paths.dest
    // TODO @Chay 验证ghostMode属性是否正确
    // ghostMode: false,
    // port: 80
  });
  gulp.watch(paths.templates.src, ["templates"]);
  gulp.watch(paths.templates.src).on("change", reload);
  gulp.watch(paths.styles.src, ["styles"]);
  gulp.watch(paths.scripts.src, ["scripts"]);
  // gulp.watch(paths.scripts.src).on('change', reload);
  gulp.watch(paths.images.src, ["images"]);
  gulp.watch(paths.data.src, ["data"]);
}

// 清空脏数据
gulp.task("clean", clean);
// 对模板文件进行处理
gulp.task("templates", templates);
// 对样式进行处理
gulp.task("styles", styles);
// 对脚本进行处理
gulp.task("scripts", scripts);
// 对图片进行处理
gulp.task("images", images);
// 只是迁移图片
gulp.task("originalImages", originalImages);
// 对视频进行处理
gulp.task("data", data);
// 构建
gulp.task("build", ["clean"], () => {
  return runSequence(["templates", "styles", "scripts", "images", "data"]);
});
gulp.task("build:test", ["clean"], () => {
  return runSequence([
    "templates",
    "styles",
    "scripts",
    "originalImages",
    "data"
  ]);
});
// 启动开发服务
gulp.task(
  "dev",
  ["templates", "styles", "scripts", "originalImages", "data"],
  serve
);
// 默认
gulp.task("default", () => {
  console.log("Hello world.");
});
